import { setToken, getToken } from '@/utils/utils'
import { login } from '@/api/login'
import { Toast } from "vue-ydui/dist/lib.rem/dialog";
export default {
  state: {
    token: getToken(),
    id: null,
    username: null,
    nickname: null,
    sex: null,
    created_at: null,
    updated_at: null,
    avatar: null,
    level: 0,
    is_identity: null
  },
  mutations: {
    initData(state) {
      state.token = ''
      setToken('')
      state.id = null
      state.username = null
      state.nickname = null
      state.sex = null
      state.created_at = null
      state.updated_at = null
      state.avatar = null
    },
    setLevel(state, value) {
      // console.log(value)
      state.level = value
    },
    setAvatar(state, value) {
      state.avatar = value
    },
    setToken(state, value) {
      state.token = value
      setToken(value)
    },
    setId(state, value) {
      state.id = value
    },
    setUsername(state, value) {
      state.username = value
    },
    setNickname(state, value) {
      state.nickname = value
    },
    setSex(state, value) {
      state.sex = value
    },
    setCreated_at(state, value) {
      state.created_at = value
    },
    setUpdated_at(state, value) {
      state.updated_at = value
    },
    setIs_identity(state, value) {
      state.is_identity = value
    },
    loginSetInfo(state, value) {
      state.token = value.token
      setToken(value.token)
      state.id = value.user.id
      state.username = value.user.username
      state.nickname = value.user.nickname
      state.sex = value.user.sex
      state.created_at = value.user.created_at
      state.updated_at = value.updated_at
    }
  },
  actions: {
    handleLogin(
      { state, commit },
      { username, password, captcha, isCaptcha, invite_code, secret }
    ) {
      commit('initData', '')
      username = username.trim()
      let data = isCaptcha
        ? {
            mobile: username,
            captcha,
            invite_code,
            secret
          }
        : {
            username,
            password,
            invite_code
          }

      return new Promise((resolve, reject) => {
        login(data)
          .then(res => {
            if (res.status === 200) {
              const data = res.data.data
              commit('loginSetInfo', data)
              // commit('setToken', data.token)
              // commit('setUsername', data.user.username)
              // commit('setId', data.user.id)
              // commit('setNickname', data.user.nickname)
              // commit('setSex', data.user.sex)
              // commit('setCreated_at', data.user.created_at)
              // commit('setUpdated_at', data.user.updated_at)
              resolve(true)
            } else {
              Toast({
                mes: res.data.message,
                timeout: 1000
              })
              resolve(false)
            }
          })
          .catch(err => {
            resolve(false)
            reject(err)
          })
      })
    },
    setUserInfo(
      { state, commit },
      { level_id, sex, avatar, nickname, is_identity }
    ) {
      commit('setLevel', level_id)
      commit('setSex', sex)
      commit('setAvatar', avatar)
      commit('setNickname', nickname)
      commit('setIs_identity', is_identity)
    }
  }
}
